package acm.蓝桥4;

import java.util.*;

public class 卡牌 {

    static int[] card;
    static int[] sort;
    static int n;


    /**
     * 从i位置数，能拿到的最大值
     */
    public static int fun(int i) {
        Queue<Integer> queue = new ArrayDeque<>();
        for (int j = 0; j < n; j++) {
            int cur = card[(i + j) % n];
            queue.add(cur);
        }
        int num = 1;
        int res = 0;
        int[] clone = sort.clone();
        int top = clone.length - 1;
        Set<Integer> set = new HashSet<>();
        while (!queue.isEmpty() && clone[top] >= num) {
            int size = queue.size();
            for (int j = 0; j < size; j++) {
                Integer poll = queue.poll();
                if (poll == num) {
                    res += num;
                    while (top >= 0 && (clone[top] == num || set.contains(clone[top]))) {
                        top--;
                    }
                    set.add(num);
                    num=1;
                    break;
                } else {
                    queue.add(poll);
                }
                num++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        card = new int[n];
        for (int i = 0; i < card.length; i++) {
            card[i] = sc.nextInt();
        }
        sort = card.clone();
        Arrays.sort(sort);
        int res = 0;
        for (int i = 0; i < card.length; i++) {
            int fun = fun(i);
            res = Math.max(res, fun);
        }
        System.out.println(res);
    }

}
